/**
 * 
 */
package org.vsg.stock.basic.service.business;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.stock.basic.domain.BalanceLine;
import org.vsg.stock.basic.domain.BalanceLineFactory;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.indicator.KLineStockIndicatorModel;
import org.vsg.stock.core.indicator.ZigIndicator;
import org.vsg.stock.core.indicator.kline.ZigIndicatorResultItem;
import org.vsg.stock.core.kline.DailyKLineStock;

/**
 * 算法逻辑
 * 1. 找出某一周期的最低点
 * 
 * 2. 先找出周期内的最低点
 * 3. 找出同一波峰或波谷的连线（要求能够接近的点最多同时，倾斜度最低）
 *    如果系谷底，尽量找低成交量的周期，如果系波峰，尽量找成交量高的周期
 * @author Bill Vison
 *
 */
public class TrendBalanceLineFinderBO extends
	OrgDataStockBalanceLineFinderBO {

	private static final Logger logger = LoggerFactory.getLogger(TrendBalanceLineFinderBO.class);

	private BalanceLineFactory factory = BalanceLineFactory.getInstance();
	
	private static final String FINDER_CODE = "trend_balance_line";
	
	@Override
	public String getFinderCode() {
		// TODO Auto-generated method stub
		return FINDER_CODE;
	}	
	
	@Override
	protected List<BalanceLine> scanLines(List<StockDailyKLine>  stkKlineList) {

		// --- scan and mark point in to list and map ---

		ZigIndicator zigInd = new ZigIndicator();
		// 过细,将无法分出波,过大找的波较少,对于价来说, 3-3.3为合适

		Map<String, java.io.Serializable > resultMap = null;
		// --- pick out all the trough ---
		zigInd.setIndicatorModel( new KLineStockIndicatorModel(stkKlineList ,KLineStockIndicatorModel.KLinePosition.LOW) );
		zigInd.setThreshold( (double)3.2 );
		resultMap  = zigInd.calculate();		
		List<ZigIndicatorResultItem> lowTroughs = (List<ZigIndicatorResultItem>) resultMap.get( ZigIndicator.ALL_TROUGH );

		
		// --- pick out all the trough ---
		zigInd.setIndicatorModel( new KLineStockIndicatorModel(stkKlineList ,KLineStockIndicatorModel.KLinePosition.OPEN) );
		zigInd.setThreshold( (double)3.2 );
		resultMap  = zigInd.calculate();		
		List<ZigIndicatorResultItem> openTroughs = (List<ZigIndicatorResultItem>) resultMap.get( ZigIndicator.ALL_TROUGH );		

		LinkedHashSet<ZigIndicatorResultItem> allTroughs = new LinkedHashSet<ZigIndicatorResultItem>();
		allTroughs.addAll( lowTroughs );
		allTroughs.addAll ( openTroughs );
		
		DailyKLineStock orginalPointKline = findOriginalDataPointKLine(allTroughs);
		
		System.out.println(orginalPointKline);
		
		List<BalanceLine> allNewLines = new Vector<BalanceLine>();			




		return allNewLines;
	}
	
	/**
	 * fine the last orginal print 
	 * @param allTroughs
	 * @return
	 */
	private DailyKLineStock findOriginalDataPointKLine(LinkedHashSet<ZigIndicatorResultItem> allTroughs ) {
		DailyKLineStock returnKline = null;
		for (ZigIndicatorResultItem item : allTroughs) {
			DailyKLineStock currentKline = (DailyKLineStock)item.getAssoRef();
			if (returnKline == null ) {
				returnKline = currentKline;
			} else {
				if (returnKline.getLow() > currentKline.getLow() ) {
					returnKline = currentKline;					
				}
			}
		}
		
		return returnKline;
	}
	
	


	
}
